﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using Studio77.SuperLink.Core;
using Microsoft.Phone.Tasks;

namespace Studio77.ForestSounds
{
    public partial class AnimalPage : PhoneApplicationPage
    {
        public AnimalPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(AnimalPage_Loaded);
        }

        void AnimalPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= new RoutedEventHandler(AnimalPage_Loaded);
            if (StageManager.Instance.LaunchCount > 5)
            {
                SmartMad.Ads.WindowsPhone7.WPF.AdView adViewHandle = new SmartMad.Ads.WindowsPhone7.WPF.AdView();
                adViewHandle.AdPositionID = "90031361";
                adViewHandle.AdInterval = 30;
                adViewHandle.AdAnimation = SmartMad.Ads.WindowsPhone7.WPF.AdAnimationType.AdAnimationFlipFromLeft;
                adViewHandle.IsDebugMode = false;
                bottomAD.Children.Add(adViewHandle);
            }
        }

        int currentIndex;

        void btnNext_Click(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(100);
            if (currentIndex < DataManager.Instance.animalInfos.Count - 1)
            {
                currentIndex++;
                UpdateUI();
                this.btnPrev.Visibility = System.Windows.Visibility.Visible;
                if (currentIndex == DataManager.Instance.animalInfos.Count - 1)
                {
                    this.btnNext.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    this.btnNext.Visibility = System.Windows.Visibility.Visible;
                }
            }

        }

        void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(100);
            if (currentIndex > 0)
            {
                currentIndex--;
                UpdateUI();
                this.btnNext.Visibility = System.Windows.Visibility.Visible;
                if (currentIndex == 0)
                {
                    this.btnPrev.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    this.btnPrev.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            currentIndex = 0;
            //显示第一个
            UpdateUI();
            GotoMarketToComment();
        }

        private void UpdateUI()
        {
            AnimalInfo info = DataManager.Instance.animalInfos[currentIndex];
            this.btnPrev.Visibility = System.Windows.Visibility.Collapsed;

            string uri = string.Empty;
            uri = "/Images/" + info.ImageName;
            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            this.imgAnimal.Source = bitmap;

            this.btnCn.Content = info.CnName;
            this.btnEn.Content = info.EnName;
        }

        private void btnCn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            AnimalInfo info = DataManager.Instance.animalInfos[currentIndex];
            SoundManager.PlayEffect(info.CnSoundId);
        }

        private void btnEn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            AnimalInfo info = DataManager.Instance.animalInfos[currentIndex];
            SoundManager.PlayEffect(info.EnSoundId);
        }

        private void imgAnimal_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            AnimalInfo info = DataManager.Instance.animalInfos[currentIndex];
            SoundManager.PlayEffect(info.AnimalSoundId);
        }


        /// <summary>
        /// 去市场评价
        /// </summary>
        private void GotoMarketToComment()
        {
            if (!StageManager.Instance.IsCommented &&
                ((StageManager.Instance.LaunchCount == 3) || StageManager.Instance.LaunchCount % 15 == 0))
            {
                if (MessageBox.Show("游戏还满意吗？去商店给个好评吧。谢谢您的支持！", "去评论吧！", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
                    marketplaceReviewTask.Show();
                    StageManager.Instance.IsCommented = true;
                }
                else
                {
                    StageManager.Instance.LaunchCount = StageManager.Instance.LaunchCount + 1;
                }
            }
        }
    }
}